package cn.kgc.controller;


import cn.kgc.pojo.NoticeInf;
import cn.kgc.service.NoticeService;

import cn.kgc.utils.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("notice")
public class NoticeController {

    private  static Logger logger = Logger.getLogger(NoticeController.class);

    @Resource
    NoticeService noticeService;


    @RequestMapping("/limit")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "title",required = false) String title,
                        @RequestParam(value = "content",required = false) String content,
                        Model model){

        PageInfo pageInfo = new PageInfo();
        if (currentPageNo !=null){
            pageInfo.setCurrentPageNo(currentPageNo);
        }
        if (pageSize!=null){
            pageInfo.setPageSize(pageSize);
        }
        int totalCount = noticeService.getCount(title,content);
        pageInfo.setTotalConut(totalCount);
        List<NoticeInf> list =noticeService.getNoticeByPage(pageInfo.getCurrentPageNo(),pageInfo.getPageSize(),title,content);
        pageInfo.setData(list);
        model.addAttribute("notice",pageInfo);

        return "noticeLimit";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("id")Integer id,Model model){
        NoticeInf noticeInf = noticeService.getNoticeInfById(id);
        model.addAttribute("notice",noticeInf);
        return "noticeUpdate";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(NoticeInf noticeInf){
        boolean flag = noticeService.updateNoticeInf(noticeInf);
        if (flag){
            return "redirect:/notice/limit";
        }else {
            return "noticeUpdate";
        }
    }
    @RequestMapping("/toDel")
    public String toDel(@RequestParam("id")Integer id){
        boolean flag = noticeService.delId(id);
        if (flag){
            return "redirect:/notice/limit";
        }else {
            return "noticeLimit";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "noticeAdd";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(NoticeInf noticeInf){
        boolean flag = noticeService.addNoticeInf(noticeInf);
        if (flag){
            return "redirect:/notice/limit";
        }else {
            return "index";
        }
    }

//    @RequestMapping("/noticeIsExist/{title}")
//    @ResponseBody
//    public JSONObject noticeIsExist(@PathVariable String title) {
//
//        NoticeInf noticeInf = noticeService.getNoticeInftitle(title);
//        JSONObject jsonObject = new JSONObject();
//        if (noticeInf == null) {
//            jsonObject.put("code", "10000");
//            jsonObject.put("msg", "可以使用");
//        } else {
//            jsonObject.put("code", "10001");
//            jsonObject.put("msg", "不可以使用");
//        }
//        return jsonObject;
//    }
}
